<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="故障详情" prop="fault">
        <el-input
          v-model="queryParams.fault"
          placeholder="请输入故障详情"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="key" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入key"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组" prop="group">
        <el-select v-model="value" placeholder="请选择"  @change="getList">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
           >
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="添加时间" prop="time">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.time"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择添加时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:db:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-upload ref="plante"
                   :file-list="fileList"
                   :action="planteAction"
                   :headers="headers"
                   :before-upload="planteBeforeUpload"
                   :on-success="uploadExcelSuccess"
                   accept=".xls,.xlsx">
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
<!--          <div slot="tip" class="el-upload__tip">只能上传不超过 5MB 的.xls,.xlsx文件</div>-->
        </el-upload>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:db:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:db:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:db:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="id" align="center" prop="id" />-->
<!--      <el-table-column label="故障详情" align="center" prop="fault" />-->
<!--      <el-table-column label="key" align="center" prop="key" />-->
<!--      <el-table-column label="组" align="center" prop="group" />-->
<!--      <el-table-column label="添加时间" align="center" prop="time" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:db:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:db:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="组1" align="center" prop="group1" />
      <el-table-column label="组2" align="center" prop="group2" />
      <el-table-column label="组3" align="center" prop="group3" />
      <el-table-column label="组4" align="center" prop="group4" />
      <el-table-column label="组5" align="center" prop="group5" />
      <el-table-column label="组6" align="center" prop="group6" />
      <el-table-column label="组7" align="center" prop="group7" />
      <el-table-column label="组8" align="center" prop="group8" />
      <el-table-column label="组9" align="center" prop="group9" />
      <el-table-column label="组10" align="center" prop="group10" />


    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改故障数据库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="故障关键字" prop="fault">
          <el-input v-model="form.fault" placeholder="多个关键字使用-分隔" />
        </el-form-item>
        <el-form-item label="分组" prop="group">
          <el-select v-model="form.group" placeholder="请选择" >
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="添加时间" prop="time">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.time"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd HH:mm"-->
<!--            placeholder="请选择添加时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDb, getDb, delDb, addDb, updateDb, getGroups} from "@/api/system/db";
import { getToken } from '@/utils/auth'
import resize from "@/views/dashboard/mixins/resize";
export default {
  name: "Db",
  data() {
    return {
      options: [{value:'全选',label:'全选'}],
      options1: [],
      groups:[],
      value: '全选',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障数据库表格数据
      tableList: [],
      dbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fault: null,
        key: null,
        time: null,
        group:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      planteAction: process.env.VUE_APP_BASE_API+'/system/excel/uploadDb/',
      fileList: [],
    };
  },
  computed: {
    headers() {
      const authorization = getToken() || ''
      return { authorization }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    planteBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 5
      if (!isRightSize) {
        this.$message.error('文件大小超过 5MB')
      }
      let isAccept = new RegExp('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet|application/vnd.ms-excel').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择.xls,.xlsx类型的文件')
      }
      return isRightSize && isAccept
    },
    uploadExcelSuccess(response){
      this.$message.success('上传成功!')
      this.$refs.plante.clearFiles();
      this.getList();
      this.reset();
    },
    /** 查询故障数据库列表 */
    getList() {
      getGroups().then(response => {
        for (let i=0;i<response.length;i++){
          this.options.push({
            value:response[i],
            label:response[i]
          })
          this.options1.push({
            value:response[i],
            label:response[i]
          })
        }
      })
      if(this.value === '全选'){
        this.queryParams.group = null
      }else {
        this.queryParams.group = this.value
      }
      this.loading = true;
      listDb(this.queryParams).then(response => {
        this.dbList = response.rows;
        let groups = {group1:'',group2:'',group3:'',group4:'',group5:'',group6:'',group7:'',group8:'',group9:'',}
        for (let i=0;i<this.dbList.length;i++){
          if (this.dbList[i].group === '组10'){
            groups.group10 = this.dbList[i].fault
            this.tableList.push(groups)
            console.log(this.tableList)
            groups = {group1:'',group2:'',group3:'',group4:'',group5:'',group6:'',group7:'',group8:'',group9:'',}
            continue;
          }
          if (this.dbList[i].group === '组1'){
            groups.group1 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组2'){
            groups.group2 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组3'){
            groups.group3 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组4'){
            groups.group4 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组5'){
            groups.group5 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组6'){
            groups.group6 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组7'){
            groups.group7 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组8'){
            groups.group8 = this.dbList[i].fault
          }else if (this.dbList[i].group === '组9'){
            groups.group9 = this.dbList[i].fault
          }
        }
        console.log(this.tableList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fault: null,
        key: null,
        time: null,
        fileList:[],

      };
      this.value = '全选'
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.value = '全选'
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加故障数据库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDb(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改故障数据库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除故障数据库编号为"' + ids + '"的数据项？').then(function() {
        return delDb(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/db/export', {
        ...this.queryParams
      }, `db_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
